<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;
use think\Db;
/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    // public function index()
    // {
    //     $seventtime = \fast\Date::unixtime('day', -7);
    //     $paylist = $createlist = [];
    //     for ($i = 0; $i < 7; $i++)
    //     {
    //         $day = date("Y-m-d", $seventtime + ($i * 86400));
    //         $createlist[$day] = mt_rand(20, 200);
    //         $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
    //     }
    //     $hooks = config('addons.hooks');
    //     $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
    //     $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
    //     Config::parse($addonComposerCfg, "json", "composer");
    //     $config = Config::get("composer");
    //     $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
    //     $this->view->assign([
    //         'totaluser'        => 35200,
    //         'totalviews'       => 219390,
    //         'totalorder'       => 32143,
    //         'totalorderamount' => 174800,
    //         'todayuserlogin'   => 321,
    //         'todayusersignup'  => 430,
    //         'todayorder'       => 2324,
    //         'unsettleorder'    => 132,
    //         'sevendnu'         => '80%',
    //         'sevendau'         => '32%',
    //         'paylist'          => $paylist,
    //         'createlist'       => $createlist,
    //         'addonversion'       => $addonVersion,
    //         'uploadmode'       => $uploadmode
    //     ]);

    //     return $this->view->fetch();
    // }

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $createlist[$day] = mt_rand(20, 200);
            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        $beginTime=mktime(0,0,0,date('m'),date('d'),date('Y'));
        //今日注册数
        $user_today=Db::name('user')->where('createtime','>=',$beginTime)->count();
        //总会员数
        $user_all=Db::name('user')->count();
        //今日访客
        $visitor_today=Db::name('visitor')->where('view_time','>=',$beginTime)->count();
        //总访客数
        $visitor_all=Db::name('visitor')->count();
        //今日浏览量
        $view_today=Db::name('view')->where('view_time','>=',$beginTime)->value('count');
        $view_today=$view_today?$view_today:0;
        //总浏览量
        $view_all=Db::name('view')->sum('count');
        $view_all=$view_all?$view_all:0;
        //今日订单
        $order_today=Db::name('order')->where('createtime','>=',$beginTime)->count();
        //总订单数
        $order_all=Db::name('order')->count();
        $this->view->assign([
            'user_today'        => $user_today,
            'user_all'       => $user_all,
            'visitor_today'       => $visitor_today,
            'visitor_all' => $visitor_all,
            'view_today'   => $view_today,
            'view_all'  => $view_all,
            'order_today'       => $order_today,
            'order_all'    => $order_all,
        ]);

        return $this->view->fetch();
    }

}
